package com.jizhi.onework.utils;

import java.util.List;

/*
 * 设计一个泛型类，存储分页相关数据
 */
public class PageBean<T> {
    private List<T> beanList;//数据库查询
    private int pageIndex;//当前页，索引页，客户请求参数
    private int pageSize;//页大小，每页显示的数据条数，业务规定
    private int totalRecored;//总记录条数
    private int totalPage;//总页数
    private int pageBegin;
    private int pageEnd;
    private String url;//封装请求路径参数，Servlet赋值


    /*
     *
     */
    public void setPageBeginANDPageEnd() {
//		不够十页
        if (getTotalPage() < 10) {
            pageBegin = 1;
            pageEnd = getTotalPage();
        } else {
            /*
             * 正常情况
             */
            pageBegin = pageIndex - 5;
            pageEnd = pageIndex + 4;


            /*
             * 特殊情况
             */
//			上边界越界
            if (pageIndex - 5 < 1) {
                pageBegin = 1;
                pageEnd = 10;
            }

//			下边界越界
            if (pageIndex + 4 > getTotalPage()) {
                pageBegin = getTotalPage() - 9;
                pageEnd = getTotalPage();
            }
        }
    }

    public int getTotalPage() {
        return totalRecored % pageSize == 0 ? totalRecored / pageSize : totalRecored / pageSize + 1;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<T> getBeanList() {
        return beanList;
    }

    public void setBeanList(List<T> beanList) {
        this.beanList = beanList;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecored() {
        return totalRecored;
    }

    public void setTotalRecored(int totalRecored) {
        this.totalRecored = totalRecored;
    }

    public int getPageBegin() {
        return pageBegin;
    }

    public void setPageBegin(int pageBegin) {
        this.pageBegin = pageBegin;
    }

    public int getPageEnd() {
        return pageEnd;
    }

    public void setPageEnd(int pageEnd) {
        this.pageEnd = pageEnd;
    }


    @Override
    public String toString() {
        return "PageBean [beanList=" + beanList + ", pageIndex=" + pageIndex + ", pageSize=" + pageSize
                + ", totalRecored=" + totalRecored + ", totalPage=" + getTotalPage() + ", pageBegin=" + pageBegin
                + ", pageEnd=" + pageEnd + "]";
    }


    public PageBean() {
        super();
        // TODO Auto-generated constructor stub
    }


    public PageBean(List<T> beanList, int pageIndex, int pageSize, int totalRecored, int totalPage, int pageBegin,
                    int pageEnd, String url) {
        super();
        this.beanList = beanList;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalRecored = totalRecored;
        this.totalPage = totalPage;
        this.pageBegin = pageBegin;
        this.pageEnd = pageEnd;
        this.url = url;
    }


}
